﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using agile_tool.userControl;

namespace agile_tool
{
    /// <summary>
    /// Interaction logic for taskPage.xaml
    /// </summary>
    public partial class taskPage : UserControl, IGenericPagesData
    {
        Button createNewTask;
        create createNewTaskItem;
        int userStoryId;
        public string navigationName = "";
        public taskPage(int userStoryId, string filter,string userStoryName)
        {
            InitializeComponent();
            this.userStoryId = userStoryId;
            navigationName = userStoryName + " Task";
            refresh(filter);
        }
        public string getNavigationName()
        {
            return navigationName;
        }
        public int getId()
        {
            return userStoryId;
        }
        public void refresh(string filter)
        {
            lbTask.Items.Clear();
            
            try
            {

                DataClasses1DataContext db = new DataClasses1DataContext();
                var task = from p in db.tblTasks where p.userStoryId == userStoryId select p;
                 var resources = from q in db.tblResources select q;


                foreach (var p in task)
                {
                    if (filter == "")
                    {
                        taskItem theTaskItem = new taskItem();
                        theTaskItem.tbName.Text = p.name;
                        theTaskItem.tbDesc.Text = p.description;
                        theTaskItem.id = p.id;
                        theTaskItem.tbExpectedHours.Text = p.expectedHours.ToString();
                        theTaskItem.btnDell.Click += new RoutedEventHandler(btnDell_Click);
                        theTaskItem.btnEdit.Click += new RoutedEventHandler(btnEdit_Click);
                       
                        foreach (var r in resources)
                        {
                        ComboBoxItem cbi = new ComboBoxItem();
                        cbi.Tag = r.id;
                        cbi.Content = r.name;
                        theTaskItem.cbResource.Items.Add(cbi);
                        if (r.id == p.resourceId)
                            cbi.IsSelected = true;
                        }
                        theTaskItem.cbResource.SelectionChanged += new SelectionChangedEventHandler(cbResource_SelectionChanged);
                        lbTask.Items.Add(theTaskItem);
                    }
                    else
                    {
                        if (p.name.IndexOf(filter) != -1)
                        {
                            taskItem theTaskItem = new taskItem();
                            theTaskItem.tbName.Text = p.name;
                            theTaskItem.tbDesc.Text = p.description;
                            theTaskItem.id = p.id;
                            theTaskItem.tbExpectedHours.Text = p.expectedHours.ToString();
                            theTaskItem.btnDell.Click += new RoutedEventHandler(btnDell_Click);
                            theTaskItem.btnEdit.Click += new RoutedEventHandler(btnEdit_Click);


                            foreach (var r in resources)
                            {
                                ComboBoxItem cbi = new ComboBoxItem();
                                cbi.Tag = r.id;
                                cbi.Content = r.name;
                                theTaskItem.cbResource.Items.Add(cbi);
                                if (r.id == p.resourceId)
                                    cbi.IsSelected = true;
                            }
                            theTaskItem.cbResource.SelectionChanged += new SelectionChangedEventHandler(cbResource_SelectionChanged);
                        
                            lbTask.Items.Add(theTaskItem);
                        }
                    }

                }
            }
            catch { }
            createNewTask = new Button { Content = "Create New Task" };
            createNewTask.Click += new RoutedEventHandler(createNewTask_Click);
            createNewTask.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            lbTask.Items.Add(createNewTask);
        }

        void cbResource_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int resourceId = int.Parse(((ComboBoxItem)((ComboBox)sender).SelectedItem).Tag.ToString());
            taskItem theTaskItem = new taskItem();
            theTaskItem = (taskItem)helper.FindParrentUserControl((DependencyObject)sender, theTaskItem.GetType().ToString());
            DataClasses1DataContext db = new DataClasses1DataContext();
            tblTask tb = (from p in db.tblTasks where p.id == theTaskItem.id select p).First();
            tb.resourceId = resourceId;
            db.SubmitChanges();
        }



        void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            taskItem theTaskItem = new taskItem();
            theTaskItem = (taskItem)helper.FindParrentUserControl((Button)sender, theTaskItem.GetType().ToString());
            if (theTaskItem.spSlide.Height == 0)
            {
                theTaskItem.openTheDesc();
                theTaskItem.btnEdit.Content = "Save";
            }
            else
            {
                if (theTaskItem.tbName.Text == "")
                { MessageBox.Show("The Name Can't be Empty"); }
                else
                {
                    theTaskItem.closeTheDesc();
                    theTaskItem.btnEdit.Content = "Edit";
                    DataClasses1DataContext db = new DataClasses1DataContext();
                    tblTask tb = (from p in db.tblTasks where p.id == theTaskItem.id select p).First();
                    tb.name = theTaskItem.tbName.Text;
                    tb.description = theTaskItem.tbDesc.Text;
                    tb.expectedHours = int.Parse(theTaskItem.tbExpectedHours.Text);
                    db.SubmitChanges();
                }
            }
        }

        void btnDell_Click(object sender, RoutedEventArgs e)
        {
             taskItem theTaskItem = new taskItem();
            theTaskItem = (taskItem)helper.FindParrentUserControl(((Button)sender), theTaskItem.GetType().ToString());
            DataClasses1DataContext db = new DataClasses1DataContext();
            try
            {
                var tbUserSp = from p in db.tblUserStorySprints where p.userStoryId == userStoryId select p;
                tbUserSp.First();
                MessageBox.Show("You Can't Delete task Which it's User Story is Assigned to Sprintt");
            }
            catch
            {
                var tsakHour = from p in db.tblTaskHours where p.taskId ==theTaskItem.id select p;
                db.tblTaskHours.DeleteAllOnSubmit(tsakHour);
                db.SubmitChanges();

                tblTask tb = (from p in db.tblTasks where p.id == theTaskItem.id select p).First();
                db.tblTasks.DeleteOnSubmit(tb);
                db.SubmitChanges();


                lbTask.Items.Remove(theTaskItem);
            }
        }

        void createNewTask_Click(object sender, RoutedEventArgs e)
        {
            lbTask.Items.Remove(createNewTask);
            createNewTaskItem = new create();
            createNewTaskItem.btnCancel.Click += new RoutedEventHandler(btnCancel_Click);
            createNewTaskItem.btnSave.Click += new RoutedEventHandler(btnSave_Click);

            lbTask.Items.Add(createNewTaskItem);
        }

        void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (createNewTaskItem.tbName.Text != "")
            {
                DataClasses1DataContext db = new DataClasses1DataContext();
                tblTask tp = new tblTask { name = createNewTaskItem.tbName.Text, description = createNewTaskItem.tbDesc.Text, userStoryId = userStoryId  };
                db.tblTasks.InsertOnSubmit(tp);
                db.SubmitChanges();

                refresh("");
            }
            else
            {
                MessageBox.Show("You need a name for your Task");
            }
        }

        void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            refresh("");
        }
    }
}
